package com.qianfeng.vhly.youshare.adapters;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import com.qianfeng.vhly.youshare.Constants;
import com.qianfeng.vhly.youshare.R;
import com.qianfeng.vhly.youshare.model.Product;
import com.qianfeng.vhly.youshare.utils.FileCache;
import com.qianfeng.vhly.youshare.utils.ImageCache;
import com.qianfeng.vhly.youshare.utils.ImageLoader;

import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * User: vhly[FR]
 * Date: 15/2/7
 * Email: vhly@163.com
 */
public class StorePlatformAdapter extends BaseAdapter {

    // 1. 设置数据

    private List<Product> productList;

    // 2. 准备布局

    private LayoutInflater inflater;

    public StorePlatformAdapter(Context context, List<Product> products) {
        if (context == null) {
            throw new IllegalArgumentException("Context must be set.");
        }
        this.productList = products;
        inflater = LayoutInflater.from(context);
    }

    @Override
    public int getCount() {
        int ret = 0;
        if (productList != null) {
            ret = productList.size();
        }
        return ret;
    }

    @Override
    public Object getItem(int position) {
        Object ret = null;
        if (productList != null) {
            ret = productList.get(position);
        }
        return ret;
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View ret = null;

        if (convertView != null) {
            ret = convertView;
        } else {
            // TODO 制作界面
            ret = inflater.inflate(R.layout.item_store_platform, parent, false);
        }

        ViewHolder holder = (ViewHolder) ret.getTag();

        if (holder == null) {
            holder = new ViewHolder();

            holder.imgIcon = (ImageView) ret.findViewById(R.id.item_platform_icon);
            holder.txtTitle = (TextView) ret.findViewById(R.id.item_platform_title);
            holder.txtPrice = (TextView) ret.findViewById(R.id.item_platform_price);

            ret.setTag(holder);
        }

        // 设置数据的部分

        Product product = productList.get(position);

        holder.txtTitle.setText(product.getGoodsName());

        holder.txtPrice.setText(product.getShopPrice());

        // TODO 进行图片的加载

        // 1. 获取缩略图地址
        // 2. 检查内存缓存
        // 3. 检查文件缓存
        // 4. 网络加载数据
        // 5. 更新缓存

        holder.imgIcon.setImageResource(R.drawable.product_default);

        String thumb = product.getGoodsThumb();

        if (thumb != null && thumb.length() > 0) {
            if (!thumb.startsWith("http://") && !thumb.startsWith("https://")) {
                thumb = Constants.IMAGE_SERVER + thumb;
            }

            ImageCache imageCache = ImageCache.getInstance();

            Bitmap bitmap = imageCache.getImage(thumb);

            if (bitmap != null) {
                holder.imgIcon.setImageBitmap(bitmap);
            } else {
                // TODO 查看文件缓存
                FileCache fileCache = FileCache.getInstance();

                byte[] content = fileCache.getContent(thumb);

                if (content != null && content.length > 0) {
                    // 生成 Bitmap ，并且更新UI

                    bitmap = BitmapFactory.decodeByteArray(content, 0, content.length);

                    imageCache.putImage(thumb, bitmap);

                    holder.imgIcon.setImageBitmap(bitmap);
                }else{
                    // TODO 进行网络下载

                    ImageLoader task = new ImageLoader(holder.imgIcon);
                    task.execute(thumb);

                }

            }

        }


        return ret;
    }

    private static class ViewHolder {
        public ImageView imgIcon;
        public TextView txtTitle;
        public TextView txtPrice;
    }

}
